package com.zlq.iwork.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.mail.MessagingException;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zlq.iwork.R;
import com.zlq.iwork.mail.MultiMailsender;
import com.zlq.iwork.mail.MultiMailsender.MultiMailSenderInfo;
import com.zlq.iwork.utils.MarkWithText;
import com.zlq.iwork.utils.ToolAlert;
import com.zlq.iwork.utils.ToolDateTime;
import com.zlq.iwork.utils.ToolFile;
import com.zlq.iwork.utils.XWord;

public class WorkListAdapter extends BaseAdapter {

	private Context mContext;
	private List<Map<String, String>> mList;
	private LayoutInflater mInflater;
	private String name;

	Handler handler=new Handler() {
		
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case 0:
				ToolAlert.loading(mContext, "正在发送邮件...");
				break;
			case 1:
				ToolAlert.dialog(mContext, "邮件发送成功!");
				break;
			case 2:
				ToolAlert.dialog(mContext, "邮件发送失败!");
				break;
			default:
				break;
			}
		}
	};
	
	public WorkListAdapter(Context context, List<Map<String, String>> list) {
		this.mContext = context;
		this.mList = list;
		this.mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Map<String, String> getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.list_works_item, null);

			holder.p_user = (ImageView) convertView.findViewById(R.id.p_user);
			holder.u_name = (TextView) convertView.findViewById(R.id.u_name);
			holder.work_date = (TextView) convertView
					.findViewById(R.id.work_date);
			holder.work_content = (TextView) convertView
					.findViewById(R.id.work_content);
			holder.send = (TextView) convertView.findViewById(R.id.send);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		bindView(position, holder);
		return convertView;
	}

	private void bindView(int position, final ViewHolder holder) {
		Map<String, String> map = getItem(position);
		if (map == null) {
			return;
		}
		holder.u_name.setText(map.get("name"));
		holder.work_date.setText(map.get("date"));
		holder.work_content.setText(map.get("note"));
		holder.send.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sendNote(holder);

			}
		});

	}

	private void sendNote(final ViewHolder holder) {
		List<MarkWithText> list = new ArrayList<MarkWithText>();

		list.add(new MarkWithText("[dept]", ToolFile
				.getValFromDefSharedPreferences(mContext, "dept", "软件部")));
		list.add(new MarkWithText("[name]", holder.u_name.getText().toString()));
		list.add(new MarkWithText("[ndate]", holder.work_date.getText()
				.toString()));
		list.add(new MarkWithText("[week]", "星期"
				+ ToolDateTime.getWeek(holder.work_date.getText().toString())));
		list.add(new MarkWithText("[note]", holder.work_content.getText()
				.toString()));
		XWord xWord = new XWord();
		name = holder.u_name.getText().toString() + " 工作日志"
				+ holder.work_date.getText().toString() + ".doc";
		boolean re = xWord.DoWordXmlAndSave(mContext,
				ToolFile.readAssetsValue(mContext, "note.xml"), name, list);
		if (re) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					sendMail(holder);
				}
			}).start();
		} else {
			Toast.makeText(mContext, "失败", Toast.LENGTH_SHORT).show();
		}
	}

	private void sendMail(ViewHolder holder) {
		// 这个类主要是设置邮件
		MultiMailSenderInfo mailInfo = new MultiMailSenderInfo();
		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(mContext);
		mailInfo.setMailServerHost(sp.getString("p_smtp", ""));
		mailInfo.setMailServerPort("25");
		mailInfo.setValidate(true);
		mailInfo.setUserName(sp.getString("p_email", ""));
		mailInfo.setPassword(sp.getString("p_mail_pass", ""));// 您的邮箱密码
		mailInfo.setFromAddress(sp.getString("p_email", ""));
		mailInfo.setSubject(sp.getString("p_name", "") + " 工作日志"
				+ holder.work_date.getText().toString());
		mailInfo.setContent("工作日志");
		// mailInfo.setAttachFileNames(fileNames)
		String[] receivers = sp.getString("p_recevice", "").split(",");
		
		List<String> list=new ArrayList<String>();
		for (String s : receivers) {
			list.add(s);
		}
		mailInfo.setToAddress(list.get(0));
		list.remove(0);
		// String[] ccs = receivers;
		if (list.size()>0) {
			
			mailInfo.setReceivers(list.toArray(new String[list.size()]));
		}
		// 附件
		String[] fileNames = { mContext.getFilesDir() + File.separator + name };
		mailInfo.setAttachFileNames(fileNames);
		handler.sendEmptyMessage(0);
		// mailInfo.setCcs(ccs);
		// 这个类主要来发送邮件
		//MultiMailsender sms = new MultiMailsender();
		try {
			MultiMailsender.sendMailtoMultiReceiver(mailInfo);
			handler.sendEmptyMessage(1);
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			handler.sendEmptyMessage(2);
			Looper.prepare();
			Toast.makeText(mContext,"邮件发送失败"+ e.toString(), Toast.LENGTH_LONG).show();
			Looper.loop();
		}// 发送文体格式
		// MultiMailsender.sendHtmlMail(mailInfo);//发送html格式
		// MultiMailsender.sendMailtoMultiCC(mailInfo);//发送抄送
	}

	class ViewHolder {
		public ImageView p_user;
		public TextView u_name;
		public TextView work_date;
		public TextView work_content;
		public TextView send;
	}
}
